package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public Result addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

        //1.判断购物车里是否已有该商品，有则商品数量加1
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtil.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.query(shoppingCart);
        if (!list.isEmpty()) {
            ShoppingCart commodity = list.get(0);
            commodity.setNumber(commodity.getNumber() + 1);
            shoppingCartMapper.update(commodity);
            return Result.success();
        }

        //2.没有，则判断是套餐还是菜品，并设置相关属性
        Long setmealId = shoppingCartDTO.getSetmealId();
        if (setmealId != null) {
            SetmealVO setmealVO = setmealMapper.selectById(setmealId);
            shoppingCart.setAmount(setmealVO.getPrice());
            shoppingCart.setName(setmealVO.getName());
            shoppingCart.setImage(setmealVO.getImage());
        } else {
            DishVO dishVO = dishMapper.selectById(shoppingCartDTO.getDishId());
            shoppingCart.setAmount(dishVO.getPrice());
            shoppingCart.setName(dishVO.getName());
            shoppingCart.setImage(dishVO.getImage());
        }
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setNumber(1);

        //插入数据到购物车数据库
        shoppingCartMapper.sava(shoppingCart);
        return Result.success();
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public Result<List<ShoppingCart>> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.query(shoppingCart);
        return Result.success(list);
    }

    /**
     * 删除购物车中的一个商品
     *
     * @param shoppingCartDTO
     * @return
     */
    @Override
    public Result delete(ShoppingCartDTO shoppingCartDTO) {

        //判断购物车中该商品数量是否大于1，大于1则数量减1
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtil.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.query(shoppingCart);
        ShoppingCart commodity = list.get(0);
        Integer number = commodity.getNumber();
        if(number >1){
            commodity.setNumber(number-1);
            shoppingCartMapper.update(commodity);
            return Result.success();
        }

        //等于1，直接从购物车中删除
        shoppingCartMapper.delete(shoppingCart);

        return Result.success();
    }

    /**
     * 清空购物车
     * @return
     */
    @Override
    public Result clean() {
        //设置用户id属性
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //清空该用户购物车
        shoppingCartMapper.delete(shoppingCart);
        return Result.success();
    }
}
